(function($){
    
    $.fn.extend({
        
        truncate: function(options){
            var defaults = {
                            length: 300,
                            mintrail: 20,
                            moreText: "more",
                            lessText: "less",
                            ellipsisText: "..."
            };
            options = $.extend(defaults, options);
            
            
            return this.each(function(){
                
                obj = $(this);
                var body = obj.html();
                        
                if(body.length > options.length + options.mintrail)
                {
                    
                    var splitLocation = body.indexOf(' ', options.length);
                    if(splitLocation != -1)
                    {
                        var splitLocation = body.indexOf(' ', options.length);
                        
                        var str1 = body.substring(0, splitLocation + 1);
                        var str2 = body.substring(splitLocation, body.length - 1);
                        obj.html(str1 + '<span class="truncate_ellipsis">' + options.ellipsisText + '</span>' + '<span class="truncate_more">' + str2 + '</span>');
                        obj.find('.truncate_more').css("display", "none");
                        obj.append(
                            '<div class="clearnoth" style="display: inline-block;">' +
                            '<a href="#" class="truncate_more_link" >' +
                            options.moreText + '</a>' +
                            '</div>'
                        );
                        var moreLink = $('.truncate_more_link', obj);
                        var moreContent = $('.truncate_more', obj);
                        var ellipsis = $('.truncate_ellipsis', obj);
                        moreLink.click(function(){
                            if(moreLink.text() == options.moreText)
                            {
                                moreContent.fadeIn();
                                moreLink.text(options.lessText);
                                ellipsis.css("display", "none");
                            }
                            else{
                                moreContent.fadeOut();
                                moreLink.text(options.moreText);
                                ellipsis.css("display", "inline");
                            };
                            return false;
                        });
                    };
                };
            });
            
            
        },  // end truncate
        
   
        tabs : function(options){
            var defaults = {
                            animate: 'none',
            };
            options = $.extend(defaults, options);
            return this.each(function(){
                var obj = $(this);
                obj.css({'border':'1px solid #C9C9C9'});
                obj.addClass("corner-top");
                var headings = $('h2', obj);
                var tabcontent = headings.next();
                headings.hide();
                tabcontent.hide();
                tabcontent.eq(0).show();
                obj.prepend('<ul class="tabs"></ul>');
                headings.each(function(){
                    var label = $(this).text();
                    $("ul.tabs", obj).append("<li>" + label + "</li>");
                });
                $("ul.tabs", obj).addClass("corner-top");
                $("div p", obj).css({'margin':'10px'});
                var tabs = $("ul.tabs li", obj);
                tabs.addClass('corner-top');
                tabs.eq(0).addClass("sel");
                var tabs = $("ul.tabs li", obj);
                tabs.click(function(){
                    
                    if(options.animate == 'slide'){
                        tabs.removeClass("sel");
                        tabcontent.slideUp();
                        $(this).addClass("sel");
                        var current = tabs.index($(this));
                        tabcontent.eq(current).slideDown();
                    }
                    else if(options.animate == 'fade') {
                        tabs.removeClass("sel");
                        tabcontent.hide();
                        $(this).addClass("sel");
                        var current = tabs.index($(this));
                        tabcontent.eq(current).fadeIn(600);
                    }
                    else{
                        tabs.removeClass("sel");
                        tabcontent.hide();
                        $(this).addClass("sel");
                        var current = tabs.index($(this));
                        tabcontent.eq(current).show();
                    }
                    
                    
                    
                });
            });
        }, // end of tabs function
    
    
    });
    
	
	
	
	
	//the real jQuery plugin
    
    var methods = {
	
	init : function( options ) {
   
            var defaults = {
                            modal: false,
                            width: 500,
                            message: 'Message',
                            buttons: {}
                            
            };
            options = $.extend(defaults, options);
            return this.each(function(){
		
                obj = $(this);
                title = obj.attr("title");
		if(obj.html() !== '' || obj.html() !== null){
		    options.messaage = obj.html();
		}
		obj.css("z-index","1000").css("position","absolute");
                obj.width(options.width);
                obj.html('<div class="dialog corner-all shadow" style="width: ' + options.width +'px;">' +
                            '<div class="title corner-all">' +
                                '<h3>' + title +'</h3>' +
                                '<span class="close" >&times;</span>' +
                                '<div style="clear:both"></div>' +
                            '</div>' +
                            '<div class="dialog-body">' +
                                '<p>' + options.message +
                                '</p>' +
                            '</div>' +
                            '<div class="dialog-button">' +
                                '' +
                            '</div>' +
                        '</div>');
                
                obj.removeAttr("title");
		var modcon = $('<div id="modal" class="modal-backdrop"></div>');
                modcon.appendTo('body');
                if(options.modal == true){
                    modcon.show();
                }
                $('.dialog', obj).position({
                            my: 'center',
                            at: 'center',
                            of: '#modal'
                });
		
		function close(){
		    $('.dialog', obj).hide();
		    modcon.hide();
		    obj.attr('title', title);
		}
                $('.close', obj).click(function(){
                    close();
                });
		
		
                var self = this,
			hasButtons = false;
                if (typeof options.buttons === 'object' && options.buttons !== null) {
			$.each(options.buttons, function() {
				return !(hasButtons = true);
			});
		}
                if (hasButtons) {
                    $.each(options.buttons, function(name, props) {
                        var button = $('<input type="button" class="btn btn-green" value="'+ name +'" />')
                                        .click(function() {
						props();
                                        })
                                    .appendTo('.dialog-button');  
                                    
                    });
                }
                else{
                     var button = $('<input type="button" class="btn btn-green" value="Ok" />')
                                        .click(function() {
						close();
                                        })
                                    .appendTo('.dialog-button');  
                }
            });
        }, //end of dialog function,
     close : function( ) {

       return this.each(function(){
        $('.dialog', obj).hide();
	modcon.hide();
	obj.attr('title', title);

       })

     },
     reposition : function( ) {  },
     show : function( ) {  },
     hide : function( ) { },
     update : function( content ) { }
  };

  $.fn.dialog = function( method ) {
    
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };
	
    
})(jQuery);